package com.android.yummycookies.activeBoard;

import android.graphics.Canvas;

import com.android.yummycookies.commonInterface.IDrawable;
import com.android.yummycookies.commonInterface.IUpdatable;
import com.android.yummycookies.model.commonModel.ObserversPool;
import com.android.yummycookies.model.cookie.Cookie;
import com.android.yummycookies.util.Constant;
import com.android.yummycookies.util.Event;
import com.android.yummycookies.util.Validator;

public class CookieBoard implements IDrawable, IUpdatable{

	public static final String TAG = CookieBoard.class.getSimpleName();

	public static final int BOARD_FULL = Event.BOARD_FULL;
	public static final int BOARD_MIN_HEIGHT_COOKIES = Constant.BOARD_MIN_HEIGHT_COOKIES;
	public static final int BOARD_MAX_HEIGHT_COOKIES = Constant.BOARD_MAX_HEIGHT_COOKIES;
	public static final int BOARD_MIN_WIDTH_COOKIES = Constant.BOARD_MIN_WIDTH_COOKIES;
	public static final int BOARD_MAX_WIDTH_COOKIES = Constant.BOARD_MAX_WIDTH_COOKIES;

	/** Nombre max de cookies en hauteur */
	private final int maxHeightCookies;
	/** Nombre max de cookies en largeur */
	private final int maxWidthCookies;
	
	private final int cookiesHeigt;
	private final int cookiesWidth;
	
	private Cookie[][] cookies;
	private ObserversPool observers = new ObserversPool();

	public CookieBoard(int maxHeightCookies, int maxWidthCookies, int cookiesHeight, int cookiesWidth) throws IllegalArgumentException {
		
		Validator.checkGreaterThan(TAG, "max height cookies number", maxHeightCookies, "min height cookies number constant", BOARD_MIN_HEIGHT_COOKIES);
		Validator.checkLesserThan(TAG, "max height cookies number", maxHeightCookies, "max height cookies number constant", BOARD_MAX_HEIGHT_COOKIES);
		
		Validator.checkGreaterThan(TAG, "max width cookies number", maxWidthCookies, "min width cookies number", BOARD_MIN_WIDTH_COOKIES);
		Validator.checkLesserThan(TAG, "max width cookies number", maxWidthCookies, "max width cookies number", BOARD_MAX_WIDTH_COOKIES);
		
		Validator.checkIsPositive(TAG, "cookie's height", cookiesHeight);
		Validator.checkIsPositive(TAG, "cookie's width", cookiesWidth);
		
		cookies = new Cookie[maxHeightCookies][maxWidthCookies];
		this.maxHeightCookies = maxHeightCookies;
		this.maxWidthCookies = maxWidthCookies;
		this.cookiesHeigt = cookiesHeight;
		this.cookiesWidth = cookiesWidth;
	}
	
	public ObserversPool getObservers() {
		return observers;
	}

	/**
	 * Remplit le plateau de cookies
	 * 
	 * @param x
	 *            l'abscisse du coin haut gauche du nouveau plateau de cookies
	 * @param y
	 *            l'ordonnee du coin haut gauche du nouveau plateau de cookies
	 * @param cookies
	 *            un tableau bidimensionnel de cookies
	 */
	public void setCookieBoard(int x, int y, Cookie[][] cookies) {

		// verifie que le plateau de cookies est bien inscrit
		// dans le cookieBoard en tenant compte des coordonnees
		// de placement

		// verifie le nombre de lignes
		Validator.checkGreaterThan(TAG, "max y coordinate", y + cookies.length, "cookieBoard height size", maxHeightCookies);
		
		// verifie le nombre de colonnes
		for (int i = 0; i < maxWidthCookies; i++) {
			Validator.checkGreaterThan(TAG, "max x coordinate", x + cookies[i].length, "cookieBoard width size", this.cookies[i].length);
		}

		// supprime tous les éléments du plateau
		for (int i = 0; i < maxHeightCookies; i++) {
			for (int j = 0; j < maxWidthCookies; j++) {
				this.cookies[i][j] = null;
			}
		}
		
		// ajout des nouveaux elements au plateau
		for (int i = 0; i < cookies.length; i++) {
			for (int j = 0; j < cookies[i].length; j++) {
				Cookie c = cookies[i][j];
				// calcul les coordonnees du cookie
				c.setX((j + x) * cookiesWidth);
				c.setY((i + y)* cookiesHeigt);
				// ajoute le cookie
				this.cookies[y + i][x + j] = c;
			}
		}
	}

	public void draw(Canvas canvas) {
		for (int i = 0; i < maxHeightCookies; i++) {
			for (int j = 0; j < maxWidthCookies; j++) {
				//cookies[i][j].draw(canvas);
			}
		}
	}

	@Override
	public void update() {
		//??? update du board
	}

}
